package com.longteng.diamond.controller;


import com.longteng.diamond.domain.InterfaceMockData;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.po.TablePage;
import com.longteng.diamond.service.InterfaceMockDataService;
import com.longteng.diamond.util.ValidUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;


/**
 * Created by Administrator
 * DATE : 2018/8/5
 * TIME : 10:34
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Controller
@Slf4j
@SessionAttributes(value = {"matchType","interfaceMockId"})
@RequestMapping("/interfaceMockDataController")
public class InterfaceMockDataController {
    @Autowired
    InterfaceMockDataService interfaceMockDataService;
    @Autowired
    ValidUtil validUtil;
    @RequestMapping("/interfaceMockDataPage")
    public ModelAndView openMockDataPage(Integer interfaceMockId, String matchType,HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        if(null!=interfaceMockId || null!=matchType) {
            Integer sessionMockId=(Integer) session.getAttribute("interfaceMockId");
            if(null==sessionMockId||!interfaceMockId.equals(sessionMockId)) {
                modelAndView.addObject("interfaceMockId", interfaceMockId);
            }
            String sessionMatchType=(String) session.getAttribute("matchType");
            if(null==sessionMatchType||!matchType.equals(sessionMatchType)) {
                modelAndView.addObject("matchType", matchType);
            }
        }
        //2.table优化
        modelAndView.setViewName("page/interfaceMockData/interfaceMockDataPage");
        return modelAndView;
    }

    @RequestMapping("/getInterfaceMockData")
    public @ResponseBody
    TablePage getInterfaceMockData(InterfaceMockData interfaceMockData) {
        TablePage tablePage = new TablePage();
        try {
            tablePage = interfaceMockDataService.getInterfaceMockData(interfaceMockData);
        } catch (Exception e) {
            log.error("获取用户错误:", e);
        }
        return tablePage;
    }

    @RequestMapping("/addInterfaceMockData")
    public ModelAndView addInterfaceMockData(@Valid InterfaceMockData interfaceMockData, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        if (!validUtil.checkParam(interfaceMockData.getClass().getDeclaredFields(), bindingResult, modelAndView)) {
            modelAndView.setViewName("page/interfaceMockData/editInterfaceMockDataPage");
            return modelAndView;
        }
        try {
            interfaceMockDataService.addInterfaceMockData(interfaceMockData);
        } catch (Exception e) {
            log.error("新增接口错误", e);
            modelAndView.setViewName("/interfaceMockDataController/editInterfaceMockData");
            modelAndView.addObject("errorMsg", e.toString());
            return modelAndView;
        }
        modelAndView.setViewName("redirect:/interfaceMockDataController/interfaceMockDataPage");
        return modelAndView;
    }


    @RequestMapping("/updateInterfaceMockData")
    public ModelAndView updateInterfaceMockData(@Valid InterfaceMockData interfaceMockData, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        if (!validUtil.checkParam(interfaceMockData.getClass().getDeclaredFields(), bindingResult, modelAndView)) {
            modelAndView.setViewName("page/interfaceMockData/editInterfaceMockDataPage");
            return modelAndView;
        }
        try {
            interfaceMockDataService.updateInterfaceMockData(interfaceMockData);
        } catch (Exception e) {
            log.error("更新接口错误", e);
        }
        modelAndView.setViewName("redirect:/interfaceMockDataController/interfaceMockDataPage");
        return modelAndView;
    }

    @RequestMapping("/editInterfaceMockDataPage")
    public ModelAndView editInterfaceMockDataPage(InterfaceMockData interfaceMockData) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/interfaceMockData/editInterfaceMockDataPage");
        if (null != interfaceMockData.getInterfaceMockDataId() && interfaceMockData.getInterfaceMockDataId() > 0) {
            interfaceMockData = interfaceMockDataService.findInterfaceMockDataById(interfaceMockData.getInterfaceMockDataId());
        }
        modelAndView.addObject("interfaceMockData", interfaceMockData);
        return modelAndView;
    }

    @RequestMapping("/deleteInterfaceMockData")
    public @ResponseBody
    RequestResult deleteInterfaceMockData(Integer interfaceMockDataId) {
        RequestResult requestResult = new RequestResult();
        try {
            interfaceMockDataService.deleteInterfaceMockDataById(interfaceMockDataId);
            requestResult.setSuccess(true);
        } catch (Exception e) {
            requestResult.setSuccess(false);
            requestResult.setMsg("删除接口异常,请联系管理员");
            log.error("删除接口异常", e);
        }
        return requestResult;
    }
}
